/***************************

DESCRIPTION: Adds a disease flag to reshaped files

Datasets used:
 (1)`year'\medpar_all_file_reshaped.dta
 (2) `year'\outpatient_base_claims_reshaped.dta
 (3) `year'\bcarrier_claims_j_reshaped.dta

Datasets created:
 (1)`year'\medpar_all_file_flagged.dta
 (2) `year'\outpatient_base_claims_flagged.dta
 (3) `year'\bcarrier_claims_j_flagged.dta


***************************/

set more off
capture log close
global path "N:\MedicareClaims-P045601-BE\Work\"
global dpath "N:\MedicareClaims-P045601-BE\Work\ay_data"
global mfixformat "%02.0f"
log using reshape_method_pt2, replace


local diabetes_out 355 379 362 369 354 367 368 331 781 410 /// 
	429 428 436 427 459 785 413 414 444 453 V81 424 440 ///
	458 447 599 593 586 590 596 592 782 707 709 686 883 ///
	879 882 892 891 893 884
local hyp_out 410 413 414 427 428 429 436 444 459 785
local chronic 714 715 716 721 729 724 722 732 733 696 707 703 700 216 172 ///
	173 366 365 361 571 572 531 532 533 550 551 552 553 530 535 787 555 556 558 ///
	562 564 534 537 560 569 151 152 153 154 240 241 243 244 245 246 250 280 281 ///
	282 283 284 285 345 346 784 592 590 581 582 583 593 596 600 602 340 614 615 ///
	616 620 621 622 623 624 626 617 618 619 625 627 174 179 180 181 182 183 184 ///
	185 217 218 219 220 221 390 392 393 394 395 396 397 398 399 410 411 412 413 ///
	414 745 746 415 416 417 423 424 426 428 401 402 403 404 405 430 431 432 433 ///
	434 435 437 440 442 451 454 455 490 491 493 477 471 473 470 474 476 492 511 ///
	500 501 502 503 504 505 011 019 515 518 160 161 163 X00 X01 X02 X03 X05 X06 ///
	X07 X08 X09 X10 X11 X12 X19 X20 X21 X23 X24 X22 X25 X26 X28 X27 X29 X30 X31 ///
	X32 X34 X35 X40 X41 X46 X42 X43 X44 X45 X47 X48 X49 X50 X51 X56 X52 X53 X54 ///
	X55 X57 X58 X59 X60 X61 X62 X63 X64 X70 X71 X80 X74 X84 X73 X77 X78 X75 X76 ///
	X85 X86 X79 X89 X91   
local hyp 401 405 642
local diabetes 250


local chronic5 7110 7119 7128 7242 7243 7269 7300 7301 7302 7303 7309 7310 ///
	7312 7271 7260 7261 7262 7263 7264 7265 7266 7267 7268 7270 7272 7273 7274 ///
	7275 7276 7277 7278 7279 7062 7061 7060 6989 3622 3623 3624 3625 3625 3626 ///
	3627 3628 3629 3685 3883 5730 5733 5734 5735 5736 5737 5738 5739 5360 5361 ///
	5362 5363 5364 5365 5366 5367 5369 5641 2421 2422 2423 2424 2428 2429 7292 ///
	5950 5951 5952 5953 5958 5959 6010 6012 6013 6015 6016 6017 6018 6019 6280 ////
	6282 6283 6284 6285 6286 6287 6299 4296 4270 4271 4273 7850 7852 4274 4275 ///
	4276 4277 4278 4279 7851 4209 4210 4219 4229 4250 4251 4252 4253 4254 4255 ///
	4259 4290 4291 4292 4293 4410 4411 4412 4413 4414 4415 4416 4598 4599 1622 ///
	1623 1624 1625 1626 1627 1627 1628 1629 1620
	
local hyp5 4160 4593 5723 7962 9979 3482


foreach year of numlist 2005/2012 {
	*OUTPATIENT
	use "$path\`year'\outpatient_base_claims_j_reshaped.dta", clear
	gen keep=0
	foreach i in diabetes_out hyp_out chronic hyp diabetes {
		foreach j of local `i' {
			bysort BENE_ID CLM_THRU_DT : egen `i'_`j' = max(substr(ICD_DGNS_CD, 1, 3) == "`j'")
			 replace keep=1 if `i'_`j'==1
		}
	}
		foreach i in chronic5 hyp5 {
			foreach j of local `i' {
				bysort BENE_ID CLM_THRU_DT : egen `i'_`j' = max(substr(ICD_DGNS_CD, 1, 4) == "`j'")
				 replace keep=1 if `i'_`j'==1
		}
	}
	
	keep if keep==1
	
	save "$path\`year'\outpatient_base_claims_j_flagged.dta", replace 


	*CARRIER FILES (ambulatory)
	use "{$path}\`year'\bcarrier_claims_j_reshaped.dta", clear
	foreach i in diabetes_out hyp_out chronic hyp diabetes {
		foreach j of local `i' {
			bysort BENE_ID CLM_THRU_DT : egen `i'_`j' = max(substr(ICD_DGNS_CD, 1, 3) == "`j'")
			replace keep=1 if `i'_`j'==1
		}
	}
		foreach i in chronic5 hyp5 {
		foreach j of local `i' {
			bysort BENE_ID CLM_THRU_DT : egen `i'_`j' = max(substr(ICD_DGNS_CD, 1, 4) == "`j'")
			replace keep=1 if `i'_`j'==1
		}
	}
	keep if keep==1
	save "{$path}\`year'\bcarrier_claims_j_flagged.dta", replace 


	*INPATIENT
	use "$path\`year'\medpar_all_file_reshaped.dta", clear
	foreach i in diabetes_out hyp_out chronic hyp diabetes {
		foreach j of local `i' {
			bysort BENE_ID CLM_THRU_DT : egen `i'_`j' = max(substr(ICD_DGNS_CD, 1, 3) == "`j'")
			replace keep=1 if `i'_`j'==1
		}
	}
	
		foreach i in chronic5 hyp5 {
		foreach j of local `i' {
			bysort BENE_ID CLM_THRU_DT : egen `i'_`j' = max(substr(ICD_DGNS_CD, 1, 4) == "`j'")
			replace keep=1 if `i'_`j'==1
		}
	}
	keep if keep==1
	save "$path\`year'\medpar_all_file_flagged.dta", replace 
}



